extends IBasicComponent


class_name RangeUiProgress


@export var range_property: String = ""
@export var progress_bar: ProgressBar = null
@export var character: CharacterComponent = null


var range_attr: IRangeAttribute = null


func __check_exports() -> Dictionary:
    return IComponent.merge({
        "character": IComponent.find_up.bind(self, func(x): return x is CharacterComponent, character),
        "progress_bar": [IComponent.find_down.bind(self, func(x): return x is Range, progress_bar)],
    }, super.__check_exports())

func __prepare() -> void:
    super.__prepare()

    range_attr = character.meta.items.get(range_property)
    if range_attr:
        character.meta.event_property_changed.connect(__on_property_changed)
    __update_progress()

func __on_property_changed(attrs: CustomAttributes, attr: Attribute, property: String, new_value: int) -> void:
    if attr == range_attr:
        __update_progress()

func __update_progress() -> void:
    if progress_bar and range_attr:
        progress_bar.value = progress_bar.max_value * range_attr.value / range_attr.max_value
